﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using StoryMaker.Common;
using StoryMaker.Plugins;

namespace StoryMaker.Application
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = Startup.Instance.Container.GetExportedValue<StoryMakerViewModel>();
        }

        public StoryMakerViewModel ViewModel
        {
            get { return DataContext as StoryMakerViewModel; }
            set { DataContext = value; }
        }

        private void ContentControl_MouseMove(object sender, MouseEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            StoryShape shape = control.Content as StoryShape;
            if (shape != null && e.LeftButton == MouseButtonState.Pressed)
            {
                DragDrop.DoDragDrop(control,
                                     shape,
                                     DragDropEffects.Copy);
            }
        }

        private void ItemsControl_Drop(object sender, DragEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            StoryShape shape = control.Content as StoryShape;
            if (shape != null)
            {
                ViewModel.CurrentPage.Shapes.Add(shape);
            }
        }

        private void CanvasItemControl_DragOver(object sender, DragEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            if (control != null)
            {
                StoryShape shape = control.Content as StoryShape;
                if (shape != null)
                {
                    control.AllowDrop = true;
                }
            }
        }

        private void CanvasItemControl_DragEnter(object sender, DragEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            if (control != null)
            {
                StoryShape shape = control.Content as StoryShape;
                if (shape != null)
                {
                    control.AllowDrop = true;
                }
            }
        }

        private void CanvasItemControl_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            ContentControl control = sender as ContentControl;
            if (control != null)
            {
                StoryShape shape = control.Content as StoryShape;
                if (shape != null)
                {
                    control.AllowDrop = true;
                }
            }
        }
    }
}
